package com.googlecode.delivery.config;

import java.io.File;
import java.io.IOException;

import com.googlecode.delivery.DeliveryException;
import com.googlecode.delivery.config.impl.ConfigurationCmd;
import com.googlecode.delivery.config.impl.ConfigurationHelper;
import com.googlecode.delivery.core.impl.EnvironmentHelper;
import com.googlecode.delivery.model.DeploymentUnit;
import com.googlecode.delivery.model.Environment;


/**
 * Configuration Command Line main.
 * 
 * @author Stephen Kay
 */
public class ConfigurationLauncher {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			IConfigurationCmd cmd = create(args);
			System.out.println("---------------------");
			System.out.println("-- Configure");
			System.out.println("---------------------");
			System.out.println("Artefact: " + cmd.getArtefact());
			System.out.println("Environment: " + cmd.getEnvironment());
			System.out.println("Output Dir: " + cmd.getOutputDir());
			System.out.println("---------------------");
			DeploymentUnit du = ConfigurationHelper.create(cmd.getArtefact());
			Environment env = EnvironmentHelper.create(cmd.getEnvironment());
			File outputDir = cmd.getOutputDir();
			File zipFile = ConfigurationHelper.configure(du, env, outputDir);
			System.out.println("------------------------------------");
			System.out.println("Configuration Status : SUCCESSSFUL");
			System.out.println("");
			System.out.println("Output: " + zipFile.getAbsolutePath());
			System.out.println("------------------------------------");
		} catch (DeliveryException exp) {
			System.err.println("Configuration Failed: ");
			System.err.println("  " + exp.getMessage());
		} catch (IOException ioe) {
			System.err.println("Configuration Failed: ");
			System.err.println("  " + ioe.getMessage());
		}
	}

	/**
	 * Calls the Configuration Command implementation.
	 * 
	 * @param args
	 * @return the Configuration launch Command implementation.
	 */
	static IConfigurationCmd create(String args[]) throws IOException,
			DeliveryException {
		ConfigurationCmd cmd = new ConfigurationCmd();
		try {
			// parse the command line arguments
			cmd.parse(args);
		} catch (DeliveryException exp) {
			// oops, something went wrong
			cmd.printHelp();
			System.err.println("Configuration Failed: ");
			System.err.println("  " + exp.getMessage());
		}
		return cmd;
	}
}
